package com.tgky.ybzb.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tgky.ybzb.common.PageParam;

public class MPUtil {
    public MPUtil() {
    }

    public static Page getPage(PageParam pageParam) {
        if (pageParam == null) {
            pageParam = new PageParam();
        }

        Page page = new Page((long)pageParam.getPageCurrent(), (long)pageParam.getPageSize());
        if (pageParam.getOrderBy() != null && !pageParam.getOrderBy().isEmpty()) {
            String[] orderColumnArr = pageParam.getOrderBy().split(",");

            for(int i = 0; i < orderColumnArr.length; ++i) {
                orderColumnArr[i] = CommonUtil.camelToUnderline(orderColumnArr[i]);
            }

            if (pageParam.getIsAsc()) {
                page.addOrder(OrderItem.ascs(orderColumnArr));
            } else {
                page.addOrder(OrderItem.descs(orderColumnArr));
            }
        }

        return page;
    }

    public static QueryWrapper getQueryWrapper() {
        return new QueryWrapper();
    }

    public static QueryWrapper getQueryWrapper(PageParam pageParam) {
        if (pageParam == null) {
            return new QueryWrapper();
        } else {
            QueryWrapper queryWrapper = new QueryWrapper();
            if (pageParam.getOrderBy() != null && !pageParam.getOrderBy().isEmpty()) {
                String[] orderColumnArr = pageParam.getOrderBy().split(",");

                for(int i = 0; i < orderColumnArr.length; ++i) {
                    orderColumnArr[i] = CommonUtil.camelToUnderline(orderColumnArr[i]);
                }

                if (pageParam.getIsAsc()) {
                    queryWrapper.orderByAsc(orderColumnArr);
                } else {
                    queryWrapper.orderByDesc(orderColumnArr);
                }
            }

            return queryWrapper;
        }
    }
}